package com.hdc.hdc.paltform.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdc.hdc.paltform.entity.vo.AjaxReturnVO;
import com.hdc.hdc.paltform.service.AdvertServiceI;
import com.hdc.hdc.paltform.util.IsNotNullValit;

/**
 * 平台首页
 * @author GJY
 * 人生格言：学以致用终将成功
 * @version 创建时间：2018年4月2日 上午10:01:26 
 *
 */
@Controller
public class IndexController {
	
	private final Integer pageSize = 16;
	
	@Autowired
	private AdvertServiceI advertServiceI;
	
	@GetMapping("/")
	public String gotoIndex() {
		
		return "index/index";
	}
	
	@GetMapping("/bodyIndex")
	public String gotoIndexBody() {
		return "index/body_index";
	}
	
	@GetMapping("/getAllAdvert")
	@ResponseBody
	public AjaxReturnVO getAllAdvert(@RequestParam("page")Integer page) {
		
		AjaxReturnVO rj = new AjaxReturnVO();
		
		if (!IsNotNullValit.IntegerIsNotNull(page)) {
			page = 1;
		}
		
		try {
			
			Map<String,Object> result = this.advertServiceI.getBlivos(page,pageSize);
			
			if (result != null) {
				rj.setOk(true);
				rj.setAttrs(result);
				rj.setData(page);
			}else {
				rj.setOk(false);
				rj.setData(page>1?page-1:page);
				rj.setMsg("没有数据啦!");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			rj.setOk(false);
			rj.setMsg("查询失败:"+e.getMessage());
		}
		
		return rj;
	}

	@GetMapping("/getAllAdvertForPage")
	@ResponseBody
	public AjaxReturnVO getAllAdvertForPage() {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			j.setOk(true);
			j.setData(this.advertServiceI.findAdvertForPage());
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
			j.setMsg("查询异常");
		}
		
		return j;
		
	}
	
	
}
